<?php
/**
 * 汇友科技
 * @copyright (c) 2017 http://www.huiyou365.net All rights reserved.
 * @author: Creatint <creatint@163.com>
 */

namespace Admin\Model;

use Think\Model;
use Closure;

class WechatMenuModel extends Model {
    public function get($query = null, $field = true, $order = 'sort ASC', Closure $closure = null){
        $map = array();

        $query['id']>0 && $map['id'] = $query['id'];
        unset($query['id']);
        foreach($query as $k=>$v){
            $map[$k] = $v;
        }

        if ( $query['id']>0 ) {
            $res = $this
                ->where($map)
                ->field($field)
                ->find();
        } else {
            $res = $this
                ->where($map)
                ->order($order)
                ->field($field)
                ->select();
        }

        if ( $closure ) {
            $res = $closure($res);
        }

        return $res;
    }

    public function getTree($query = null, $field = true, $order = 'sort ASC',Closure $closure = null){

        $list = $this->get($query, $field, $order, $closure);
        $list = list_to_tree($list, $pk = 'id', $pid = 'pid', $child = 'sub_button', $root = 0);

        return $list;
    }
}